package com.kaiyidiannao.common.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by david on 25/04/14.
 */
public class DateTimeUtils {
    /**
     * 时间的格式化 (时间戳转换为时间)
     * <p>格式:1999-02-13 12:30 或 13-02-13 12:30</p>
     * @param date
     * @return
     */
    public static String timeFormat(String date) {
        if (TextUtils.isEmpty(date)) {
            return "";
        }
        Date d = null;
        try {
            d = new Date(Long.valueOf(date) * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(d == null) {
            return "";
        }
        String result = new SimpleDateFormat("yyyy.MM.dd").format(d);//yyyy-MM-dd HH:mm
        return result;
    }

    /**
     * 字符串转日期：(2002-10-8 15:30:22要把它转成日期)
     * @return
     */
    public static Date stringToDate(String dataStr) throws ParseException {
        SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dataStr)  ;
        return date;
    }

    /**
     * 日期转字符串
     * @param date
     * @return
     */
    public static String DateToString (Date date) {
        SimpleDateFormat sdf  =   new  SimpleDateFormat( "yyyy.MM.dd HH:mm:ss" );
        return sdf.format(date);
    }

    /**
     * 计算时间差 返回分钟
     *
     * @param begin
     * @param end
     * @return
     */
    public static long countTime(String begin,String end){
        int day = 0;
        int hour = 0;
        int minute = 0;
        long total_minute = 0;
        StringBuffer sb = new StringBuffer();

//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date begin_date = df.parse(begin);
            Date end_date = df.parse(end);

            total_minute = (end_date.getTime() - begin_date.getTime())/(1000*60);

//            hour = (int) total_minute/60;
//            minute = (int) total_minute%60;

        } catch (ParseException e) {
            System.out.println("传入的时间格式不符合规定");
        }

        //sb.append("工作时间为：").append(hour).append("小时").append(minute).append("分钟");
        //return sb.toString();
        return total_minute;
    }

}
